#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <map>
using namespace std;

class Solution {
public:
    int countValidSelections(vector<int>& nums) {
        int ZeroCount = 0;
        for (int i = 0; i < nums.size(); ++i)
        {
            if (nums[i] == 0)
            {
                ZeroCount += ZeroNums(nums, i, -1);
                ZeroCount += ZeroNums(nums, i, 1);
            }
        }

        return ZeroCount;
    }

    int ZeroNums(vector<int> vv, int curr, int flag)
    {
        while (curr >= 0 && curr < vv.size())
        {
            curr += flag;
            if (curr < 0 || curr >= vv.size())
                break;

            if (vv[curr] != 0)
            {
                vv[curr]--;
                flag *= -1;
                curr += flag;
            }
        }

        for (auto& e : vv)
        {
            if (e)
                return 0;
        }

        return 1;
    }
};